#include "def.h"
#include "2440addr.h"
#include "ADC.H" 
#include "POLUART.H"

volatile uint32 preScaler;

void ADCInit(uint32 freq)
{
	if(freq>2500000)
		freq = 2500000;
	if(freq==0)
		freq = 1250000;
	preScaler = 50000000/freq - 1;
}
uint16 ADCRead(uint8 ch)
{
    uint32 ret;
    
	rADCCON = (1<<14)|(preScaler<<6)|(ch<<3);

    rADCCON |= 0x1;   //start ADC	
	
	for(ret=0;ret<2000;ret++)
		if(!(rADCCON & 0x1)) break;
	for(ret=0;ret<200000;ret++)
		if((rADCCON & (1<<15))) break;
	
	ret = ((uint16)rADCDAT0 & 0x3ff);
	
	return ret;	
}